//
//  MoviePlayViewController.m
//  LZMPro
//
//  Created by dllo on 15/11/5.
//  Copyright © 2015年 LZS. All rights reserved.
//

#import "MoviePlayViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#import "MovieModel.h"
#import <MBProgressHUD.h>

@interface MoviePlayViewController ()

@property(nonatomic, retain) UIView *topView;
@property(nonatomic, retain) UIView *downView;
@property(nonatomic, retain) UISlider *scheduleSlider;
@property(nonatomic, retain) MPMoviePlayerController *mpMoviewV;
@property(nonatomic, retain) NSTimer *timer;
@property(nonatomic, retain) UILabel *timeLabel;
@property(nonatomic, retain) UIButton *playButton;
@property(nonatomic, retain) UILabel *titleLabel;
@property(nonatomic, retain) UIView *MBview;
@property(nonatomic, retain) MPMoviePlayerViewController *mpVC;
//定义一个BOOL值记录是否选择
@property(nonatomic, assign) BOOL isSelected;

//菊花和上拉下拉刷新
@property(nonatomic, retain)MBProgressHUD *HUD;

@end

@implementation MoviePlayViewController

-(void)setIndex:(NSInteger)index{
    if (_index != index) {
        _index = index;
    }
    
    [self playMovie];
}

- (BOOL)shouldAutorotate{
    return NO;
} //NS_AVAILABLE_IOS(6_0);当前viewcontroller是否支持转屏

//- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
//
//    return UIInterfaceOrientationMaskLandscape;
//} //当前viewcontroller支持哪些转屏方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientations{
   return UIInterfaceOrientationMaskLandscape;
}
-(UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationLandscapeLeft;
}//默认的屏幕方向

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.mpVC= [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL URLWithString:self.VedioPlayUrl]];
    _mpVC.view.frame = CGRectMake(0, 0, 375, 667);
    
    self.mpMoviewV = _mpVC.moviePlayer;
    _mpMoviewV.controlStyle = MPMovieControlStyleNone;
    self.isSelected = YES;
    
    
    
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 667, 60)];
    self.topView.backgroundColor = [UIColor blackColor];
    self.topView.alpha = 0.8;
    [_mpVC.view addSubview:self.topView];
    
    UIButton *button =[UIButton buttonWithType:(UIButtonTypeCustom)];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:(UIControlEventTouchUpInside)];
    button.frame = CGRectMake(10, 15, 30,30) ;
    [button setBackgroundImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)];
    [_topView addSubview:button];
    
    self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 20, 300, 20)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.text = self.model.title;
    [self.topView addSubview:self.titleLabel];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    self.MBview = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CURRENTHEIGHT, CURRENTWIDTH )];
    [_mpVC.view addSubview:_MBview];
    [_MBview addGestureRecognizer:tap];
    
    
    
    [_mpVC.view bringSubviewToFront:self.topView];
    [_mpVC.view bringSubviewToFront:self.downView];
    
    //downView
    self.downView = [[UIView alloc] initWithFrame:CGRectMake(0, 315, CURRENTHEIGHT, 60)];
    _downView.backgroundColor = [UIColor blackColor];
    _downView.alpha = 0.5;
    [_mpVC.view addSubview:_downView];
    
    
    self.playButton =[UIButton buttonWithType:(UIButtonTypeCustom)];
    [_playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _playButton.frame = CGRectMake(320, 20, 30,30) ;
    [_playButton setBackgroundImage:[UIImage imageNamed:@"stop"] forState:(UIControlStateNormal)];
    
    UIButton *lastButton =[UIButton buttonWithType:(UIButtonTypeCustom)];
    [lastButton addTarget:self action:@selector(lastButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    lastButton.frame = CGRectMake(240, 20, 30,30) ;
    [lastButton setBackgroundImage:[UIImage imageNamed:@"preMovie1"] forState:(UIControlStateNormal)];
    [_downView addSubview:lastButton];
    
    
    UIButton *nextButton =[UIButton buttonWithType:(UIButtonTypeCustom)];
    [nextButton addTarget:self action:@selector(nextButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    nextButton.frame = CGRectMake(400, 20, 30,30) ;
    [nextButton setBackgroundImage:[UIImage imageNamed:@"nextmp4"] forState:(UIControlStateNormal)];
    
    [_downView addSubview:nextButton];
    
    self.timeLabel  =[[UILabel alloc]initWithFrame:CGRectMake(580, 10, 100, 13)];
    self.timeLabel.textColor = [UIColor whiteColor];
    self.timeLabel.font = [UIFont systemFontOfSize:12];
    
    [_downView addSubview:self.timeLabel];
    
    self.scheduleSlider = [[UISlider alloc]initWithFrame:CGRectMake(75, 10, 500, 10)];
    [_scheduleSlider addTarget:self action:@selector(sliderAction:) forControlEvents:(UIControlEventValueChanged)];
    [_scheduleSlider setThumbImage:[UIImage imageNamed:@"iconfont-jindu"] forState:(UIControlStateNormal)];
    [_downView addSubview:_scheduleSlider];
    
    
    [_downView addSubview:_playButton];
    
    [self.view addSubview:_mpVC.view];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(track) userInfo:nil repeats:YES];
    
    //自动播放状态
    if (self.mpMoviewV.playbackState !=  MPMoviePlaybackStatePlaying) {
        [self.mpMoviewV play];
    }
    
    
//    self.HUD = [MBProgressHUD showHUDAddedTo:_MBview animated:YES];
//    [_MBview addSubview:self.HUD];
//    self.HUD.delegate = self;
//    self.HUD.backgroundColor = [UIColor blackColor];
//    self.HUD.mode = MBProgressHUDModeIndeterminate;
//    self.HUD.labelText = @"加载中...";
//    [_MBview release];
    
}


//菊花效果消失
-(void)hudWasHidden:(MBProgressHUD *)hud{
    //    [hud removeFromSuperview];
    hud.alpha = 0;
    //    [hud completionBlock];
    hud = nil;
}


- (void)backAction
{
    [self dismissViewControllerAnimated:YES completion:^{
    }];
}

- (void)playMovie
{
    MovieModel *model  = [self.array objectAtIndex:_index];
    
    NSURL *url =[NSURL URLWithString:model.playUrl];
    
    [self.mpVC.view removeFromSuperview];
    [self.mpMoviewV.view removeFromSuperview];
    self.mpVC = [[MPMoviePlayerViewController alloc]initWithContentURL:url];
    _mpVC.view.frame = CGRectMake(0, 0, CURRENTWIDTH, CURRENTHEIGHT);
    self.mpMoviewV = _mpVC.moviePlayer;
    _mpMoviewV.controlStyle = MPMovieControlStyleNone;
    self.isSelected = YES;
    
    //自动播放状态
    if (self.mpMoviewV.playbackState !=  MPMoviePlaybackStatePlaying) {
        [self.mpMoviewV play];
    }
    
    float widthNumber = CURRENTWIDTH/667.00;
    float heightNumber = CURRENTHEIGHT/375.00;
    
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 667 *widthNumber, 60 *heightNumber)];
    self.topView.backgroundColor = [UIColor blackColor];
    self.topView.alpha = 0.8;
    [_mpVC.view addSubview:self.topView];
    
    
    self.titleLabel  = [[UILabel alloc]initWithFrame:CGRectMake(80 * widthNumber, 20 * heightNumber, 300 * heightNumber, 20 * heightNumber)];
    self.titleLabel.textColor = [UIColor whiteColor];
    self.titleLabel.text = model.title;
    [self.topView addSubview:self.titleLabel];
    
    UIButton *button =[UIButton buttonWithType:(UIButtonTypeCustom)];
    button.frame = CGRectMake(10 *widthNumber, 15 *heightNumber, 30 *widthNumber,30 *heightNumber) ;
    [button setBackgroundImage:[UIImage imageNamed:@"back"] forState:(UIControlStateNormal)];
    [button addTarget:self action:@selector(backAction) forControlEvents:(UIControlEventTouchUpInside)];
    [_topView addSubview:button];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    UIView  *MBview = [[UIView alloc] initWithFrame:CGRectMake(0,0, 667 * widthNumber, 375 * widthNumber)];
    [_mpVC.view addSubview:MBview];
    [MBview addGestureRecognizer:tap];
    //    self.MBview.frame = CGRectMake(0, 60 * heightNumber, CURRENTHEIGHT, CURRENTWIDTH - 120 * WIDTHSCALE);
    
    self.downView = [[UIView alloc]initWithFrame:CGRectMake(0, 375 *heightNumber-60*heightNumber, 667, 60)];
    _downView.backgroundColor = [UIColor blackColor];
    _downView.alpha = 0.5;
    [_mpVC.view addSubview:_downView];
    
    self.playButton =[UIButton buttonWithType:(UIButtonTypeCustom)];
    [_playButton addTarget:self action:@selector(playButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    _playButton.frame = CGRectMake(325 *widthNumber, 20 *heightNumber, 30 *widthNumber,30 *heightNumber) ;
    [_playButton setBackgroundImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
    [_downView addSubview:_playButton];
    
    UIButton *lastButton =[UIButton buttonWithType:(UIButtonTypeCustom)];
    [lastButton addTarget:self action:@selector(lastButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    lastButton.frame = CGRectMake(245 *widthNumber, 20 *heightNumber, 30 *widthNumber,30 *heightNumber);
    [lastButton setBackgroundImage:[UIImage imageNamed:@"preMovie1"] forState:(UIControlStateNormal)];
    [_downView addSubview:lastButton];
    
    
    UIButton *nextButton =[UIButton buttonWithType:(UIButtonTypeCustom)];
    [nextButton addTarget:self action:@selector(nextButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
    nextButton.frame = CGRectMake(405 *widthNumber, 20 *heightNumber, 30 *widthNumber,30 *heightNumber) ;
    [nextButton setBackgroundImage:[UIImage imageNamed:@"nextmp4"] forState:(UIControlStateNormal)];
    [self.downView addSubview:nextButton];
    
    self.timeLabel  =[[UILabel alloc]initWithFrame:CGRectMake(580 *widthNumber, 10 *heightNumber, 100 *widthNumber, 13 *heightNumber)];
    self.timeLabel.textColor = [UIColor whiteColor];
    self.timeLabel.font = [UIFont systemFontOfSize:12 *widthNumber - 1];
    [_downView addSubview:self.timeLabel];
    
    self.scheduleSlider = [[UISlider alloc]initWithFrame:CGRectMake(75 *widthNumber, 10*heightNumber,  500 *widthNumber, 10 *heightNumber)];
    [_scheduleSlider addTarget:self action:@selector(sliderAction:) forControlEvents:(UIControlEventValueChanged)];
    [self.scheduleSlider setThumbImage:[UIImage imageNamed:@"iconfont-jindu"] forState:(UIControlStateNormal)];
    [_downView addSubview:_scheduleSlider];
    
    [self.view addSubview:_mpVC.view];
    [_mpVC.view bringSubviewToFront:self.topView];
    [_mpVC.view bringSubviewToFront:self.downView];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(track) userInfo:nil repeats:YES];
    
//    self.HUD = [MBProgressHUD showHUDAddedTo:MBview animated:YES];
//    [MBview addSubview:self.HUD];
//    self.HUD.delegate = self;
//    self.HUD.backgroundColor = [UIColor blackColor];
//    self.HUD.mode = MBProgressHUDModeIndeterminate;
//    self.HUD.labelText = @"加载中...";
}

- (void)buttonAction
{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}

- (void)sliderAction:(UISlider *)slider
{
    
    [self.mpMoviewV setCurrentPlaybackTime:slider.value];
}

-(void)track
{
    if (self.mpMoviewV.playbackState ==  MPMoviePlaybackStatePlaying) {
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
        //隐藏菊花
        [self hudWasHidden:self.HUD];
        
    }else{
        [self.playButton setBackgroundImage:[UIImage imageNamed:@"playmovie"] forState:UIControlStateNormal];
    }
    
    if (self.mpMoviewV.playbackState == MPMoviePlaybackStateStopped) {
        //隐藏菊花
        [self hudWasHidden:self.HUD];
    }
    if (self.mpMoviewV.playbackState == MPMoviePlaybackStatePaused) {
        self.HUD.alpha = 1.0;
    }
    
    self.scheduleSlider.maximumValue = self.mpMoviewV.duration;//音乐总共时长
    self.scheduleSlider.value = self.mpMoviewV.currentPlaybackTime;//当前进度
    
    //当前时长进度progress
    NSInteger proMin = (NSInteger)self.mpMoviewV.currentPlaybackTime / 60;//当前分钟
    NSInteger proSec = (NSInteger)self.mpMoviewV.currentPlaybackTime % 60;//当前秒
    
    //duration 总时长
    NSInteger durMin = (NSInteger)self.mpMoviewV.duration / 60;//总秒
    NSInteger durSec = (NSInteger)self.mpMoviewV.duration % 60;//总分钟
    
    self.timeLabel.text = [NSString stringWithFormat:@"%02ld:%02ld / %02ld:%02ld", proMin, proSec, durMin, durSec];
    
    
    
}

- (void)nextButtonAction:(UIButton *)btn
{
    
    _index++;
    if (_index == self.array.count) {
        
        _index = 0;
    }
    
    
    MovieModel *model = [self.array objectAtIndex:_index];
    
    self.model  = model;
    self.VedioPlayUrl  = model.playUrl;
    self.titleLabel.text = model.title;
    [self.playButton  setBackgroundImage:[UIImage imageNamed:@"playmovie"] forState:(UIControlStateNormal)];
    
    [self playMovie];
    
}
- (void)lastButtonAction:(UIButton *)btn
{
    
    _index--;
    if (_index < 0 ) {
        
        _index = self.array.count - 1;
    }
    
    MovieModel *model = [self.array objectAtIndex:self.index];
    
    self.VedioPlayUrl  = model.playUrl;
    self.titleLabel.text = model.title;
    [self.playButton  setBackgroundImage:[UIImage imageNamed:@"playmovie"] forState:UIControlStateNormal];
    [self playMovie];
}

- (void)playButtonAction:(UIButton *)btn
{
    
    if (self.mpMoviewV.playbackState ==  MPMoviePlaybackStatePlaying) {
        [btn setBackgroundImage:[UIImage imageNamed:@"stop"] forState:UIControlStateNormal];
        [self.mpMoviewV pause];
    }
    else
    {
        [btn setBackgroundImage:[UIImage imageNamed:@"playmovie"] forState:UIControlStateNormal];
        [self.mpMoviewV play];
    }
}

- (void)tapAction:(UITapGestureRecognizer *)tap
{
    if (self.isSelected) {
        [UIView animateWithDuration:0.5 animations:^{
            CGRect tempRect = self.topView.frame;
            tempRect.origin.y -= 60;
            self.topView.frame = tempRect;
        }];
        
        [UIView animateWithDuration:0.5 animations:^{
            CGRect tempRect = self.downView.frame;
            tempRect.origin.y += 60;
            self.downView.frame = tempRect;
        }];
        self.isSelected = NO;
    }
    else
    {
        [UIView animateWithDuration:0.3 animations:^{
            CGRect tempRect = self.topView.frame;
            tempRect.origin.y += 60;
            self.topView.frame = tempRect;
        }];
        
        [UIView animateWithDuration:0.3 animations:^{
            CGRect tempRect = self.downView.frame;
            tempRect.origin.y -= 60;
            self.downView.frame = tempRect;
        }];
        self.isSelected = YES;
    }
}

//视图将要消失的时候
-(void)viewWillDisappear:(BOOL)animated{
    
    if (self.mpMoviewV.playbackState !=  MPMoviePlaybackStatePlaying) {
        [self.mpMoviewV pause];
    }
    //定时器停止
    [self.timer invalidate];
    [super viewWillDisappear:animated];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
